package com.zzyl.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.ResourceDto;
import com.zzyl.service.ResourceService;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.MenuVo;
import com.zzyl.vo.ResourceVo;
import com.zzyl.vo.TreeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/resource")
@Api(tags = "资源管理页面相关接口")
@RequiredArgsConstructor
public class ResourceController {

    private final ResourceService resourceService;

    /**
     * 资源列表查询
     * @param resourceDto
     * @return
     */
    @PostMapping("/list")
    @ApiOperation(value = "资源列表", notes = "资源列表")
    @ApiImplicitParam(name = "resourceDto", value = "资源DTO对象", required = true, dataType = "ResourceDto")
    @ApiOperationSupport(includeParameters = {"resourceDto.parentResourceNo", "resourceDto.resourceType"})
    public ResponseResult<List<ResourceVo>> resourceList(@RequestBody ResourceDto resourceDto) {
        log.info("resourceList:{}", resourceDto);
        List<ResourceVo> resourceList = resourceService.resourceList(resourceDto);
        return ResponseResult.success(resourceList);
    }

    /**
     * 资源树形
     * @param resourceDto
     * @return
     */
    @PostMapping("/tree")
    @ApiOperation(value = "资源树形")
    @ApiImplicitParam(name = "resourceDto", value = "资源DTO对象", required = true, dataType = "ResourceDto")
    @ApiOperationSupport(includeParameters = {"resourceDto.label"})
    public ResponseResult<TreeVo> resourceTree(@RequestBody ResourceDto resourceDto) {
        log.info("resourceDto:{}", resourceDto);
        TreeVo resourceTree = resourceService.resourceTree(resourceDto);
        return ResponseResult.success(resourceTree);
    }

    /**
     * 资源添加
     * @param resourceDto
     * @return
     */
    @PutMapping
    @ApiOperation(value = "资源添加", notes = "资源添加")
    @ApiImplicitParam(name = "resourceDto", value = "资源DTO对象", required = true, dataType = "ResourceDto")
    @ApiOperationSupport(includeParameters = {"resourceDto.dataState"
            , "resourceDto.icon"
            , "resourceDto.parentResourceNo"
            , "resourceDto.requestPath"
            , "resourceDto.resourceName"
            , "resourceDto.resourceType"
            , "resourceDto.sortNo"})
    public ResponseResult createResource(@RequestBody ResourceDto resourceDto) {
        log.info("添加资源:{}", resourceDto);
        resourceService.addResource(resourceDto);
        return ResponseResult.success();
    }

    /**
     * 资源修改
     * @param resourceDto
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "资源修改", notes = "资源修改")
    @ApiImplicitParam(name = "resourceDto", value = "资源DTO对象", required = true, dataType = "ResourceDto")
    @ApiOperationSupport(includeParameters = {"resourceDto.dataState"
            , "resourceDto.icon"
            , "resourceDto.parentResourceNo"
            , "resourceDto.requestPath"
            , "resourceDto.resourceName"
            , "resourceDto.resourceType"
            , "resourceDto.sortNo"
            ,"resourceDto.id"})
    public ResponseResult updateResource(@RequestBody ResourceDto resourceDto) {
        log.info("修改资源:{}", resourceDto);
        resourceService.updateResource(resourceDto);
        return ResponseResult.success();
    }

    /**
     * 启用禁用
     * @param resourceDto
     * @return
     */
    @PostMapping ("/enable")
    @ApiOperation(value = "资源启用禁用", notes = "资源启用禁用")
    @ApiImplicitParam(name = "resourceDto", value = "资源DTO对象", required = true, dataType = "ResourceDto")
    @ApiOperationSupport(includeParameters = {"resourceDto.dataState"
            , "resourceDto.parentResourceNo"
            , "resourceDto.resourceNo"})
    public ResponseResult<Boolean> updateStatus(@RequestBody ResourceDto resourceDto) {
        log.info("资源启用禁用:{}", resourceDto);
        Boolean result = resourceService.updateStatus(resourceDto);
        return ResponseResult.success(result);
    }

    /**
     * 删除菜单
     * @param menuId
     * @return
     */
    @DeleteMapping("/{menuId}")
    @ApiOperation(value = "删除菜单", notes = "删除菜单")
    public ResponseResult deleteResource(@PathVariable Long menuId) {
        log.info("删除菜单:{}", menuId);
        resourceService.delete(menuId);
        return ResponseResult.success();
    }

    /**
     * @return
     *  左侧菜单
     */
    @GetMapping("/menus")
    @ApiOperation(value = "左侧菜单", notes = "左侧菜单")
    public ResponseResult<List<MenuVo>> menus() {
        Long userId = UserThreadLocal.getMgtUserId();
        List<MenuVo> menus = resourceService.menus(userId);
        return ResponseResult.success(menus);
    }
}
